package cn.itcast.controller.system;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.system.Dept;
import cn.itcast.domain.system.Role;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.DeptService;
import cn.itcast.service.system.RoleService;
import cn.itcast.service.system.UserService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/system/user")
public class UserController extends BaseController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @RequestMapping(value = "/list",name ="查询所有用户数据")
    public String findAll(@RequestParam(defaultValue = "1",name = "page") int pageNum,@RequestParam(defaultValue = "10") int pageSize){
//        当前登录人所属部门的id
        PageInfo<User> pageBean = userService.findPage(getCompanyId(),pageNum,pageSize);
        request.setAttribute("page",pageBean);

        return "system/user/user-list";
    }
    @RequestMapping(value = "/toAdd",name ="进入到添加用户的页面")
    public String toAdd(){
//        查询当前登录人所在企业的所有部门数据
        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }

    @RequestMapping(value = "/edit",name ="保存用户数据") //新增或修改时都会进入这个方法
    public String edit(User user){

        if(StringUtils.isEmpty(user.getId())){
            //id为空新增 save
            user.setId(UUID.randomUUID().toString());
            user.setCompanyId(getCompanyId());
            user.setCompanyName(getCompanyName());
            user.setCreateTime(new Date());
            userService.save(user);
        }else{
            user.setUpdateTime(new Date());
            userService.update(user); //id如果有值 update
        }
//        不管新增还是修改都是要重定向到列表页面
        return "redirect:/system/user/list.do";  //redirect 重定向  在浏览器发起的请求
    }

    @RequestMapping(value = "/toUpdate",name ="进入到编辑用户页面")
    public String toUpdate(String id){
        User user = userService.findById(id);
        request.setAttribute("user",user);

        List<Dept> deptList = deptService.findAll(getCompanyId());
        request.setAttribute("deptList",deptList);
        return "system/user/user-add";
    }
    @RequestMapping(value = "/delete",name ="根据id删除用户数据")
    public String delete(String id){
        userService.delete(id);

        return "redirect:/system/user/list.do";
    }


    @RequestMapping(value = "/roleList",name ="进入到用户分配角色页面")
    public String roleList(String id){  //用户的id
//        查询当前用户
        User user = userService.findById(id);
        request.setAttribute("user",user);
//       查询当前企业的角色数据
        List<Role> roleList = roleService.findAll(getCompanyId());
        request.setAttribute("roleList",roleList);
//        查询当前用户下的角色id，查询出来后需要拼成一个字符串，角色id使用逗号隔开“1,2,3,4,”
        List<String> roleIdList  = userService.findRoleIdsByUserId(id);
       /* StringBuffer userRoleStr = new StringBuffer("");
        for (String roleId : roleIdList) {
            userRoleStr.append(roleId).append(",");
        }*/
        String userRoleStr = roleIdList.stream().collect(Collectors.joining(","));
        request.setAttribute("userRoleStr",userRoleStr);

        return "system/user/user-role";
    }

    @RequestMapping(value = "/changeRole",name ="保存用户分配角色数据")
    public String changeRole(String userid,String[] roleIds){
        userService.changeRole(userid,roleIds);  // 就是想中间表中插入数据，插入几条数据？
        return "redirect:/system/user/list.do";
    }

}
